package com.team5101.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team5101.entity.NowFilm;
import com.team5101.entity.PageDTO;
import com.team5101.entity.Show;
import com.team5101.entity.ShowFVO;
import com.team5101.service.IHotFsService;
import com.team5101.service.IShowService;
import com.team5101.utils.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/api/show")
@Controller
public class ShowController {

    @Autowired
    private IShowService iShowService;

    @Autowired
    private IHotFsService iHotFsService;

    //展示放映表细节
    @RequestMapping("/list")
    @ResponseBody
    public R getAllDeatil(PageDTO pageDTO,  ShowFVO showFVO){
        if(pageDTO.getCurPage()==null)
            pageDTO.setCurPage(1);
        if(pageDTO.getPageSize()==null)
            pageDTO.setPageSize(10);
        System.out.println(showFVO);

        Show s=new Show();
        if(showFVO.getS_playhall()!=null)
            s.setsPlayHall(showFVO.getS_playhall());
        //s.setId(1);
        List<Show> shows = iShowService.queryShow(s);

        List<ShowFVO> showFVOS = new ArrayList<ShowFVO>();

        for(Show s1:shows){
            ShowFVO sf = new ShowFVO();

            if((s1.getsDay())!=null&&(s1.getsHour())!=null){
                NowFilm  films=iHotFsService.queryFilm(s1.getOfId());
                sf.setId(s1.getId());
                sf.setS_day(s1.getsDay());
                sf.setS_hour(s1.getsHour());
                sf.setOf_id(s1.getOfId());//电影id
                sf.setName(films.getFnName());
                sf.setS_playhall(s1.getsPlayHall());
                showFVOS.add(sf);
            }

        }

        PageInfo<ShowFVO> pageInfo=new PageInfo<ShowFVO>(showFVOS);
        System.out.println(pageInfo);
        return R.ok(pageInfo);
    }


    //按照电影id 查询所有电影
    @RequestMapping("/filmsname")
    @ResponseBody
    public R getFilmNames(){
        List<NowFilm> nowFilms = iHotFsService.selectMovies();
        return R.ok(nowFilms);
    }

    //新增信息
    //展示放映表细节
    @RequestMapping("/add")
    @ResponseBody
    public R addDeatil(ShowFVO showFVO){

        System.out.println(showFVO);
        Show s=new Show();

        s.setsPlayHall(showFVO.getS_playhall());
        String[] Times = showFVO.getS_day().split("#");
        s.setsDay(Times[0].substring(Times[0].indexOf("年")+1));
        s.setsHour(Times[1]);
        s.setOfId(showFVO.getOf_id());

        Integer result = iShowService.addShow(s);
        return R.ok(result);
    }

    //根据id获取电影信息
    @RequestMapping("/querybyid")
    @ResponseBody
    public R queryByid(Integer id){
        Show s=new Show();
        if(id!=null)
            s.setId(id);
        List<Show> shows = iShowService.queryShow(s);
        NowFilm  films=iHotFsService.queryFilm(shows.get(0).getOfId());
        ShowFVO showFVO = new ShowFVO();
        showFVO.setId(shows.get(0).getId());
        showFVO.setS_day(shows.get(0).getsDay());
        showFVO.setS_hour(shows.get(0).getsHour());
        showFVO.setS_playhall(shows.get(0).getsPlayHall());
        showFVO.setOf_id(shows.get(0).getOfId());
        showFVO.setName(films.getFnName());
        System.out.println(showFVO);
        return R.ok(showFVO);
    }


    //修改信息细节
    @RequestMapping("/update")
    @ResponseBody
    public R updateDeatil(ShowFVO showFVO,Integer id){
        Show s=new Show();
        s.setsPlayHall(showFVO.getS_playhall());
        String[] Times = showFVO.getS_day().split("#");
        s.setsDay(Times[0].substring(Times[0].indexOf("年")+1));
        s.setsHour(Times[1]);
        s.setOfId(showFVO.getOf_id());
        s.setId(id);
        Integer result = iShowService.updateShow(s);
        return R.ok(result);
    }

    //删除信息细节
    @RequestMapping("/delete")
    @ResponseBody
    public R updateDeatil(Integer id){
        System.out.println(id);
        Integer result = iShowService.deleteShow(id);
        return R.ok(result);
    }


}
